<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
      <el-form-item label="订单流水号" prop="no">
        <el-input v-model="queryParams.no" placeholder="请输入订单流水号" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
       <!--<el-form-item label="订单类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择订单类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.ORDER_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.ORDER_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="交易类型" prop="tradeType">
        <el-select v-model="queryParams.tradeType" placeholder="请选择交易类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.TRADE_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="配送类型" prop="deliveryType">
        <el-select v-model="queryParams.deliveryType" placeholder="请选择配送类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.DISTRIBUTION_TYPE)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item> -->

      <el-form-item label="收件人名称" prop="receiverName">
        <el-input v-model="queryParams.receiverName" placeholder="请输入收件人名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <!-- <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['wine:supply-order:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['wine:supply-order:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="订单编号" align="center" prop="id" /> -->
      <el-table-column label="订单流水号" align="center" prop="no" width="180px" />
      <el-table-column label="订单类型" align="center" prop="type" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ORDER_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>

      <!-- <el-table-column label="订单来源终端" align="center" prop="terminal" /> -->
      <!-- <el-table-column label="卖方用户ID(卖方ID)" align="center" prop="saleUserId" /> -->
      <!-- <el-table-column label="卖方备注" align="center" prop="saleUserRemark" /> -->
      <!-- <el-table-column label="购买用户编号(买方ID)" align="center" prop="buyUserId" /> -->
      <!-- <el-table-column label="购买用户备注" align="center" prop="buyUserRemark" /> -->
      <!-- <el-table-column label="购买用户IP" align="center" prop="buyUserIp" /> -->
      <!-- <el-table-column label="取消类型" align="center" prop="cancelType" /> -->
      <!-- <el-table-column label="是否评价" align="center" prop="commentStatus" /> -->
      <!-- <el-table-column label="推广人编号" align="center" prop="brokerageUserId" /> -->
      <!-- <el-table-column label="保费是否已支付" align="center" prop="premiumStatus" /> -->
      <!-- <el-table-column label="发货凭证图" align="center" prop="deliveryImages" />-->
      <!-- <el-table-column label="发货物流公司编号" align="center" prop="logisticsId" />-->
      <!-- <el-table-column label="物流公司单号" align="center" prop="logisticsNo" />-->
      <!-- <el-table-column label="收件人名称" align="center" prop="receiverName" /> -->
      <!-- <el-table-column label="收件人手机" align="center" prop="receiverMobile" /> -->
      <!-- <el-table-column label="收件人地区编号" align="center" prop="receiverAreaId" /> -->
      <!-- <el-table-column label="收件人公司名称" align="center" prop="receiverCompanyName" /> -->
      <!-- <el-table-column label="收件人详细地址" align="center" prop="receiverDetailAddress" /> -->
      <!-- <el-table-column label="验货视频" align="center" prop="receiverVideo" /> -->
      <!-- <el-table-column label="平台操作状态" align="center" prop="operateStatus" /> -->
      <!-- <el-table-column label="平台操作备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="支付订单号" align="center" prop="payOrderId" width="160px" /> -->
      <!-- <el-table-column label="购买的供求信息" align="center" prop="saleInfoName" /> -->
      <!-- <el-table-column label="发货时间" align="center" prop="deliveryTime" :formatter="dateFormatter" width="180px" /> -->

      <el-table-column label="订单状态" align="center" prop="status" width="90px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column label="数 量" align="center" prop="productCount" />
      <el-table-column label="总 价" align="center" prop="totalPrice"  width="110px">
        <template #default="scope">
          {{ scope.row.totalPrice/100 }}
        </template>
      </el-table-column>

      <el-table-column label="保 费" align="center" prop="premiumPrice"  width="90px">
        <template #default="scope">
          {{ scope.row.premiumPrice/100 }}
        </template>
      </el-table-column>
      <el-table-column label="交易类型" align="center" prop="tradeType" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TRADE_STATUS" :value="scope.row.tradeType" />
        </template>
      </el-table-column>
      <el-table-column label="平台操作" align="center" prop="operateStatus" width="110px">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.tradeType==1 || scope.row.tradeType==3">不收款</el-tag>
          <dict-tag v-else :type="DICT_TYPE.OPERATE_STATUS" :value="scope.row.operateStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row.id)" v-hasPermi="['wine:supply-order:query']">
            详情
          </el-button>
          <el-button link type="primary" @click="open(scope.row)" v-hasPermi="['wine:supply-order:update']" v-if="scope.row.tradeType === 2 || scope.row.tradeType === 4">
            平台操作
          </el-button>
          <el-button link type="primary" v-hasPermi="['wine:supply-order:update']" v-if="scope.row.tradeType == 3">
            担责订单
          </el-button>
          <!-- <el-button link v-if="scope.row.status != 3" type="primary" @click="celarForm(scope.row.id)" v-hasPermi="['wine:supply-order:update']"></el-button> -->
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:supply-order:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>


  <Dialog v-model="dialogautia" title="编辑收款状态" top="50" align-center width="500">
    <el-form ref="formRef" :model="form">
      <el-form-item label="收款状态" prop="operateStatus">
        <el-select v-model="form.operateStatus" placeholder="请选择订单状态" clearable class="!w-420px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.OPERATE_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作备注" prop="remark">
        <el-input type="textarea" v-model="form.remark" placeholder="请输入平台操作备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogautia = false">取消</el-button>
        <el-button type="primary" @click="auditClick">
          确定
        </el-button>
      </div>
    </template>
  </Dialog>

  <!-- 
  <Dialog v-model="canceldialogVisible" title="取消订单" top="50" align-center width="500" @close=cancelresetForm>
    <el-form :model="cancelformData" :rules="cancelrules" ref="cancelRef">
      <el-form-item label="取消方式" prop="cancelType">
        <el-select placeholder="请选择" v-model="cancelformData.cancelType" class="w-80">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.ORDER_CANCEL)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="canceldialogVisible = false">取消</el-button>
        <el-button type="primary" @click="cancelauditClick">
          确定
        </el-button>
      </div>
    </template>
  </Dialog> 
  -->
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { SupplyOrderApi, SupplyOrderVO } from '@/api/wine/supplyorder'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 供求订单管理 列表 */
defineOptions({ name: 'SupplyOrder' })
const { push } = useRouter() // 路由跳转

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const dialogautia = ref(false)
const list = ref<SupplyOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  type: undefined,
  terminal: undefined,
  saleInfoId: undefined,
  saleUserId: undefined,
  saleUserRemark: undefined,
  buyUserId: undefined,
  buyUserRemark: undefined,
  buyUserIp: undefined,
  status: undefined,
  productCount: undefined,
  cancelType: undefined,
  tradeType: undefined,
  commentStatus: undefined,
  brokerageUserId: undefined,
  payOrderId: undefined,
  payStatus: undefined,
  payTime: [],
  payChannelCode: undefined,
  finishTime: [],
  cancelTime: [],
  totalPrice: undefined,
  payPrice: undefined,
  premiumPrice: undefined,
  premiumStatus: undefined,
  deliveryType: undefined,
  deliveryTime: [],
  deliveryImages: undefined,
  logisticsId: undefined,
  logisticsNo: undefined,
  receiveTime: [],
  receiverName: undefined,
  receiverMobile: undefined,
  receiverAreaId: undefined,
  receiverCompanyName: undefined,
  receiverDetailAddress: undefined,
  receiverVideo: undefined,
  operateStatus: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
//const cancelRef = ref()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupplyOrderApi.getSupplyOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


const form = ref({
  operateStatus: undefined,
  remark: undefined,
})

const open = (row?: object) => {
  form.value = JSON.parse(JSON.stringify(row))
  dialogautia.value = true
}

const auditClick = async () => {
  await SupplyOrderApi.updateSupplyOrder(form.value)
  message.success('操作成功')
  // 刷新列表
  await getList()
  dialogautia.value = false
}


// const celarForm =  async (id?: number)=>{
//   try {
//         await message.delConfirm('确定取消订单吗？','提示')
//        await SupplyOrderApi.cancelOrder(id)
//        message.success('操作成功')
//        // 刷新列表
//        await getList()
//  } catch {
//       // await getList()
//   }
// }

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const openForm = (id?: number) => {
  if (typeof id === 'number') {
    push({ name: 'SupplyorderDetail', params: { id } })
    return
  }
  // 新增
  // push({ name: 'SupplytypeAdd' })
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupplyOrderApi.deleteSupplyOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}


onActivated(async () => {
  await getList()
})

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
